import java.util.*;

public class MinStack {
    // leetcode 最小栈
    // https://leetcode.cn/problems/min-stack/description/?envType=study-plan-v2&envId=top-100-liked
    private Stack<Integer> stack, minStack;

    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int val) {
        stack.add(val);
        if (minStack.isEmpty() || val <= minStack.peek()) {
            minStack.add(val);
        }
    }

    public void pop() {
        int tmp = stack.pop();
        if (minStack.peek() == tmp) {
            minStack.pop();
        }
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        if (minStack.isEmpty()) return Integer.MAX_VALUE;
        return minStack.peek();
    }
}
